<?php
/**
 * Description of forum
 *
 * @author aquilax
 */
require 'FE_Controller.php';

class Forum extends FE_Controller{
  
  var $limitTopics = 5;

  function  __construct() {
    parent::__construct();
    $this->load->model('forum_model');
    $this->forum_model->forum_id = $this->forum_id;
    $this->forum_model->user_id = $this->user_id;
    $this->data['path'][''] = lang('Home');
  }

  function index(){
    $this->data['forums'] = $this->forum_model->getForumsAndGroups($this->forum_id, $this->user_id);
    $this->render();
  }

  function category(){
    $category_id = $this->uri->segment(2);
    $offset = $this->uri->segment(4)?$this->uri->segment(4):0;

    $category = $this->forum_model->loadCategory($category_id);
    if (!$category){
      //FIXME:Show Error Page Instead of redirect
      redirect('');
    }
    $this->data['path']['forum/'.$category['category_id'].'/'.$category['category_slug']] = $category['category_name'];
    /*FIXME: set limit in config.php*/
    $limitTopics = 5;

    $this->load->library('pagination');
    $config['base_url'] = site_url('forum/'.$category['category_id'].'/'.$category['category_slug']);
    $config['total_rows'] = $this->forum_model->getTopicsCountForCategory($category['category_id']);
    $config['uri_segment'] = 4;
    $config['per_page'] = $limitTopics;

    $this->pagination->initialize($config);

    $this->data['topics'] = $this->forum_model->getTopicsForCategory($this->forum_id, $this->user_id, $category['category_id'], $limitTopics, $offset);
    $this->data['action'] = 'category';
    $this->data['category_id'] = $category['category_id'];
    $this->render();
  }

  function topic(){
    $post_id = $this->uri->segment(2);
    $offset = $this->uri->segment(4)?$this->uri->segment(4):0;

    /*FIXME: set limit in config.php*/

    $top = $this->forum_model->getTopic($post_id);
    if(!$top){
      //FIXME:Show Error Page Instead of redirect
      redirect('');
    }
    $this->data['posts'] = $this->forum_model->getPosts($top['post_id'], $this->limitTopics, $offset);
    if(!$this->data['posts']){
      //FIXME:Show Error Page Instead of redirect
      redirect('');
    }
    $this->data['parent_id'] = $top['post_id'];
    $this->data['category_id'] = $top['category_id'];
    $this->data['path']['forum/'.$top['category_id'].'/'.$top['category_slug']] = $top['category_name'];
    $this->data['path']['topic/'.$top['post_id'].'/'.$top['post_slug']] = $top['title'];
    $this->load->library('pagination');
    $config['base_url'] = site_url('topic/'.$top['post_id'].'/'.$top['post_slug']);
    $config['total_rows'] = $this->forum_model->getPostCountForTopic($top['post_id']);
    $config['uri_segment'] = 4;
    $config['per_page'] = $this->limitTopics;

    $this->pagination->initialize($config);


    $this->data['action'] = 'topic';
    $this->load->helper('forum');
    $this->load->helper('form');
    $this->load->library('BBCode');
    $this->_setFlashCode();
    $this->data['smileys'] = $this->bbcode->GetDefaultSmileys();
    $this->render();
  }

  function post(){
    //TODO:Check Credentials; sanitaze input
    $flash_code = $this->input->post('flash_code');
    if ($flash_code != $this->session->flashdata('flash_code')){
      //TODO:Error Wrong Flash
      redirect('');
    }
    $parent_id = $this->input->post('parent_id');
    $title = $this->input->post('title');
    $body = $this->input->post('body');
    $category_id = $this->input->post('category_id');
    $res = $this->forum_model->post($parent_id, $category_id, $title, $body);
    if ($res && is_array($res)){
      list($id, $slug, $number) = $res;
      if ($parent_id){
        if ($number > 1){
          $this->load->helper('forum');
          $page = getPage($this->limitTopics, $number);
          if ($page){
            redirect('/topic/'.$parent_id.'/'.$slug.'/'.$page.'#p'.$id, 'refresh');
          }
        }
        redirect('/topic/'.$parent_id.'/'.$slug.'#p'.$id, 'refresh');
      } else {
        redirect('/topic/'.$id.'/'.$slug, 'refresh');
      }
    } else {
      //TODO: Show error
    }
  }

  function _setFlashCode(){
    $flash_code = md5('tiime'.time().rand(1,1000));
    $this->data['flash_code'] = $flash_code;
    $this->session->set_flashdata('flash_code', $flash_code);
  }

  function newtopic(){
    $category_id = $this->uri->segment(3);
    $category = $this->forum_model->loadCategory($category_id);
    if (!$category){
      //FIXME:Show Error Page Instead of redirect
      redirect('');
    }
    $this->data['path']['forum/'.$category['category_id'].'/'.$category['category_slug']] = $category['category_name'];
    $this->data['path']['forum/newtopic/'.$category['category_id']] = lang('New topic');
    $this->data['parent_id'] = 0;
    $this->data['category_id'] = $category['category_id'];
    $this->load->library('BBCode');
    $this->data['smileys'] = $this->bbcode->GetDefaultSmileys();
    $this->_setFlashCode();
    $this->load->helper('form');
    $this->render();
  }

}
?>
